package com.goldze.mvvmhabit.ui.register;

import android.app.Application;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.goldze.mvvmhabit.bean.ClassBean;
import com.goldze.mvvmhabit.bean.UpdateImgBean;
import com.goldze.mvvmhabit.data.DemoRepository;
import com.goldze.mvvmhabit.okgo.HTTPAPI;
import com.goldze.mvvmhabit.utils.GsonUtil;
import com.goldze.mvvmhabit.utils.SpUtils;
import com.lzy.okgo.callback.StringCallback;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.LogUtil;
import me.goldze.mvvmhabit.utils.ToastUtils;
import okhttp3.Call;
import okhttp3.Response;

public class RegisterViewModel extends BaseViewModel<DemoRepository> {
    //用户账号
    public ObservableField<String> userName = new ObservableField<>("");
    //用户名的绑定
    public ObservableField<String> name = new ObservableField<>("");
    //密码的绑定
    public ObservableField<String> password = new ObservableField<>("");
    //用户手机号的绑定
    public ObservableField<String> tel = new ObservableField<>("");
    //用户学校的绑定
    public ObservableField<String> schoolName = new ObservableField<>("");
    //用户学号
    public ObservableField<String> studentNum = new ObservableField<>("");
    //用户年纪
    public ObservableField<String> age = new ObservableField<>("");
    //班级
    public ObservableField<String> className = new ObservableField<>("");
    //身份证号
    public ObservableField<String> bodyCard = new ObservableField<>("");
    //用户性别
    public ObservableField<String> sex = new ObservableField<>("男");
    //按钮名称
    public ObservableField<String> tvName = new ObservableField<>("注册");

    public SingleLiveEvent<List<String>> FBI_LIST = new SingleLiveEvent<>();

    //头像
    public SingleLiveEvent<String> headUrl = new SingleLiveEvent<>();
    //头像url
    public ObservableField<String> head = new ObservableField<>();



    public String type = "register" ,classId;//register 注册 update 更新
    public List<String> classNameList = new ArrayList<>();//班级列表
    public List<ClassBean.ResultBean> classBeanList = new ArrayList<>();

    public RegisterViewModel(@NonNull Application application, DemoRepository repository) {
        super(application, repository);
        //从本地取得数据绑定到View层
        userName.set(model.getUserName());
        password.set(model.getPassword());

        type = (String) SpUtils.get("module_register","");
        LogUtil.e("type-->"+ type);

        switch (type){
            case "update":
                userName.set((String)SpUtils.get("module_username",""));
                name.set((String)SpUtils.get("module_name",""));
                tel.set((String)SpUtils.get("module_tel",""));
                schoolName.set((String)SpUtils.get("module_schoolname",""));
                age.set((String)SpUtils.get("module_age",""));
                studentNum.set((String)SpUtils.get("module_student_number",""));
                classId = (String)SpUtils.get("module_class_id","");
                className.set((String)SpUtils.get("module_class_name",""));
                bodyCard.set((String)SpUtils.get("module_body_card_num",""));
                if (!TextUtils.isEmpty((String) SpUtils.get("module_photo",""))) {
                    headUrl.setValue((String)SpUtils.get("module_photo",""));
                }
                tvName.set("更新");
                break;
                default:
        }

        getClassList();
    }

    //注册按钮点击事件
    public BindingCommand registerOnClickCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            switch (type) {
                case "update":
                    update();
                    break;
                case "register":
                    register();
                    break;
                default:
            }
        }
    });


    /**
     * 注册
     **/
    private void register() {
        if (TextUtils.isEmpty(head.get())) {
            ToastUtils.showShort("请选择头像！");
            return;
        }

        if (TextUtils.isEmpty(userName.get())) {
            ToastUtils.showShort("请输入账号！");
            return;
        }
        if (TextUtils.isEmpty(tel.get())) {
            ToastUtils.showShort("请输入手机号码！");
            return;
        }
        if (TextUtils.isEmpty(schoolName.get())) {
            ToastUtils.showShort("请输入学校！");
            return;
        }
        if (TextUtils.isEmpty(age.get())) {
            ToastUtils.showShort("请输入年纪！");
            return;
        }
        if (TextUtils.isEmpty(className.get())) {
            ToastUtils.showShort("请选择班级！");
            return;
        }
        if (TextUtils.isEmpty(studentNum.get())) {
            ToastUtils.showShort("请输入学号！");
            return;
        }
        if (TextUtils.isEmpty(password.get())) {
            ToastUtils.showShort("请输入密码！");
            return;
        }

        HTTPAPI.getInstance().register(userName.get(),name.get(), tel.get(), schoolName.get(),
                age.get(), studentNum.get(),sex.get(),password.get(), classId,className.get(),
                bodyCard.get(),new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        LogUtil.e("注册-->" + s);

                        SpUtils.put("module_username", userName.get());
                        SpUtils.put("module_name",name.get());
                        SpUtils.put("module_tel", tel.get());
                        SpUtils.put("module_schoolname", schoolName.get());
                        SpUtils.put("module_age", age.get());
                        SpUtils.put("module_student_number", studentNum.get());
                        SpUtils.put("module_class_id", classId);
                        SpUtils.put("module_class_name", className.get());
                        SpUtils.put("module_body_card_num", bodyCard.get());
                        SpUtils.put("module_photo",head.get());
                        finish();
                    }
                });

    }

    /**
     * 注册
     **/
    private void update() {
        if (TextUtils.isEmpty(head.get())) {
            ToastUtils.showShort("请选择头像！");
            return;
        }
        if (TextUtils.isEmpty(userName.get())) {
            ToastUtils.showShort("请输入账号！");
            return;
        }
        if (TextUtils.isEmpty(tel.get())) {
            ToastUtils.showShort("请输入手机号码！");
            return;
        }
        if (TextUtils.isEmpty(schoolName.get())) {
            ToastUtils.showShort("请输入学校！");
            return;
        }
        if (TextUtils.isEmpty(age.get())) {
            ToastUtils.showShort("请输入年纪！");
            return;
        }
        if (TextUtils.isEmpty(className.get())) {
            ToastUtils.showShort("请选择班级！");
            return;
        }
        if (TextUtils.isEmpty(studentNum.get())) {
            ToastUtils.showShort("请输入学号！");
            return;
        }
        if (TextUtils.isEmpty(password.get())) {
            ToastUtils.showShort("请输入密码！");
            return;
        }

        String token = (String) SpUtils.get("module_token","");

        HTTPAPI.getInstance().update(token,userName.get(),name.get(), tel.get(), schoolName.get(),
                age.get(), studentNum.get(),sex.get(),password.get(), classId,className.get(),
                bodyCard.get(),new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        LogUtil.e("更新-->" + s);

                        SpUtils.put("module_username", userName.get());
                        SpUtils.put("module_name",name.get());
                        SpUtils.put("module_tel", tel.get());
                        SpUtils.put("module_schoolname", schoolName.get());
                        SpUtils.put("module_age", age.get());
                        SpUtils.put("module_student_number", studentNum.get());
                        SpUtils.put("module_class_id", classId);
                        SpUtils.put("module_class_name", className.get());
                        SpUtils.put("module_body_card_num", bodyCard.get());
                        SpUtils.put("module_photo",head.get());

                        finish();
                    }
                });

    }

    //获取班级列表
    public void getClassList(){
        HTTPAPI.getInstance().getClassList(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        LogUtil.e("班级列表-->" + s);
                        ClassBean classBean = GsonUtil.INSTANCE.parse(s,ClassBean.class);
                        if (classBean.getCode() == 200) {
                            classBeanList = classBean.getResult();

                            classNameList.clear();
                            for(ClassBean.ResultBean resultBean : classBean.getResult()){
                                classNameList.add(resultBean.getName());
                            }
                        }

                        if (classNameList.size() != 0) {
                            FBI_LIST.setValue(classNameList);
                        }
                    }
                });

    }

    //选择班级后，为班级ID赋值
    public void setClassIdMethod(String className){
        for(ClassBean.ResultBean classBean : classBeanList){
            if (classBean.getName().equals(className)) {
                classId = classBean.getId();
            }
        }
    }

    //图片提交
    public void updateHeadImage(File file){
        String token = (String) SpUtils.get("module_token","");
        HTTPAPI.getInstance().updateHeadImg(token,file,new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtil.e("图片提交-->" + s);
                UpdateImgBean imgBean = GsonUtil.INSTANCE.parse(s,UpdateImgBean.class);
                if (imgBean.getCode() == 200) {
                    head.set(imgBean.getResult());
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
